package cc.bookcloud.tool.ocr.common.parsetext;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cc.bookcloud.tool.ocr.util.StringUtil;
import lombok.Data;

/**
 * @Desc: 全电电子发票(普通发票)
 * @Version: 2.0
 * @Author: John Goo
 * @Date: 2024/12/03 14:39
 **/
@Data
public class PtElectronicInvoiceNormalParseEntity extends OcrTextParseAbstract{

    //发票号码
    private String invoiceNo;
    //开票日期
    private String invoiceIssueDate;
    // 购方名称
    private String invoicePurchaseName;
    //	销售方名称
    private String invoiceSellerName;

    // 购方纳税人识别号
    private String  invoiceRatePurchaseId;

    //	销售方纳税人识别号
    private String invoiceSellerId;

    //	合计
    private String invoiceTotal;
    //	税额合计
    private String invoiceTaxTotal;

    // 价税合计大写
    private String invoiceTotalCoverTax	;
    //	价税合计小写
    private String invoiceTotalCoverTaxDigits;

    // 开票人
    private String  invoiceDrawer;



    public boolean isInvoiceNo(String str){
        if(StrUtil.isNumeric(str) && str.length()>=15 && str.length() <=20){
           return true;
        }else{
            return false;
        }
    }

    public Boolean invoiceNo(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        if(text.contains("发票号码:")){
            String[] arr = text.split(":");
            if(arr !=null && arr.length>1 ){
                this.invoiceNo = arr[1];
            }else{
           // if(arr !=null && arr.length>1 ){
                // 不在一条水平线上
                if(StringUtil.isEmpty(this.invoiceNo)){
                    // 尝试取上一位
                    JSONObject row1 = row.getJSONObject(pos -1);
                    String text1 = row1.getString("text");
                    // 检测是否是发票号码，20位数字
                    if(isInvoiceNo(text1)){
                        this.invoiceNo = text1;
                    }
                    // 尝试取下一位
                    if(StringUtil.isEmpty(this.invoiceNo)){
                        // 尝试取上一位
                        JSONObject row2 = row.getJSONObject(pos + 1);
                        String text2 = row2.getString("text");
                        // 检测是否是发票号码，20位数字
                        if(isInvoiceNo(text2)){
                            this.invoiceNo = text2;

                        }
                    }
                }
            }
            flag = true;
        }
        return flag;
    }
    public Boolean invoiceIssueDate(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        if(text.contains("开票日期:")){
            String[] arr = text.split(":");
            if(arr != null && arr.length >1 ){
                this.invoiceIssueDate = arr[1];
            }
            flag = true;
        }
        return flag;
    }
    public Boolean invoicePurchaseName(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        if( text.contains("称:")){
            String[] arr = text.split(":");

            if(arr != null && arr.length >1 ){
                this.invoicePurchaseName = arr[1];
            }
            flag = true;
        }

        return flag;
    }

    public Boolean invoiceSellerName(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        if( text.contains("称:")){
            String[] arr = text.split(":");
            if(arr != null && arr.length >1 ){
                this.invoiceSellerName = arr[1];
            }
            flag = true;
        }
        return flag;
    }


    public Boolean invoiceRatePurchaseId(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        if(text.contains("纳税人识别号:")){
            String[] arr = text.split(":");
            // 取数
            if(arr != null && arr.length > 1 ){
                this.invoiceRatePurchaseId = arr[1];
            }
            flag = true;
        }
        return flag;
    }

    public Boolean invoiceSellerId(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        if( text.contains("纳税人识别号:")){
            String[] arr = text.split(":");
            if(arr != null && arr.length > 1 ){
                this.invoiceSellerId = arr[1];
            }
            flag = true;
        }
        return flag;
    }

    public Boolean invoiceTotal(String text, Integer pos, JSONArray row){
        Boolean flag = true;
        return flag;
    }


    public Boolean invoiceTotalCoverTax(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        if(text.contains("价税合计(大写)")){
            // 取下一位
            JSONObject row1 = row.getJSONObject(pos + 1);
            String text1 = row1.getString("text");
            this.invoiceTotalCoverTax = text1;


            //（锚点项）取上一位 ：税额合计
            // 尝试取上一位
            JSONObject row2 = row.getJSONObject(pos -1);
            String text2 = row2.getString("text");
            if(text2.contains("￥") || text2.contains("Y")){
                this.invoiceTaxTotal = text2;
            }

            //（锚点项）取上上一位
            JSONObject row3 = row.getJSONObject(pos -2);
            String text3 = row3.getString("text");
            if(text3.contains("￥") ){
                this.invoiceTotal = text3;
            }
            flag = true;
        }
        return flag;
    }
    public Boolean invoiceTotalCoverTaxDigits(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        // （小写）
        if( text.contains("(小写)")){
            this.invoiceTotalCoverTaxDigits = text.replace("(小写)","");
            //this.invoiceTotalCoverTaxDigits = this.invoiceTotalCoverTaxDigits.replace("(小写)","");
            flag = true;
        }
        return flag;
    }
    public Boolean invoiceTaxTotal(String text, Integer pos, JSONArray row){
        Boolean flag = true;
        return flag;
    }


    public boolean invoiceDrawer(String text, Integer pos, JSONArray row){
        Boolean flag = false;
        if( text.contains("开票人:")){
            String[] arr = text.split(":");
            if(arr != null && arr.length >1 ){
                this.invoiceDrawer = arr[1];
            }
            flag = true;
        }
        return flag;
    }



    @Override
    public void type() {
        super.type = "electronic_invoice_normal";
        super.typeDesc="全电电子发票(普通发票)";
    }





    @Override
    public String[] eleTemplateArray(){
        return new String[]{
                " invoiceNo                    ,发票号码,--,--                 ",
                " invoiceIssueDate             ,开票日期,--,--                 ",
                " invoicePurchaseName          ,购方名称,--,--                 ",
                " invoiceSellerName            ,销售方名称,--,--               ",
                " invoiceRatePurchaseId        ,购方纳税人识别号,--,--          ",
                " invoiceSellerId              ,销售方纳税人识别号,--,--        ",
                " invoiceTotal                 ,合计,--,--                     ",
                " invoiceTaxTotal              ,税额合计,--,--                 ",
                " invoiceTotalCoverTax         ,价税合计大写,--,--              ",
                " invoiceTotalCoverTaxDigits   ,价税合计小写,--,--              ",
                " invoiceDrawer                ,开票人,--,--                    "
        };
    }


}
